#!/bin/bash
docker stop DeepSeek-R1-Distill-Llama-8B  && docker rm  DeepSeek-R1-Distill-Llama-8B
File_ip="168.17.100.17"
File_password="Huawei@123"
host_ip=$(ip a show enp61s0f0 | grep -oP 'inet \K[\d.]+')
echo $host_ip
echo "加载镜像"
modelname="DeepSeek-R1"
image_tag=mindie-2.0.T3.1-800I-A2-py311-openeuler24.03-lts.tar
CONTAINER="DeepSeek-R1-Distill-Llama-8B"
docker_image="2acbb0b1003d"

if ! docker images -q|grep -q $docker_image ;then
    echo "$image_tag镜像不存在"
    if ! which sshpass > /dev/null 2>&1; then
       echo "sshpass命令不存在，安装sshpass"
       yum install sshpass  -y
    fi
    echo "配置免密"
    ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa -q <<<y >/dev/null 2>&1

    if ! sshpass -p $File_password  ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@$File_ip  >/dev/null; then
        echo "密码错误"
        exit 1
    fi

    #sshpass -p $File_password  ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@$File_ip
    echo "拷贝镜像tar"
    scp -r root@$File_ip:/home/data_mnt/wubian_data/Images/MindIE/$image_tag /home
    echo "加载镜像"
    docker load -i /home/$image_tag
fi

echo "检查权重是否存在"
model_weight_path="/home/DeepSeek-R1-Distill-Llama-8B"
if [ -e $model_weight_path ]; then
    echo "文件存在"
else
   scp -r root@$File_ip/home/data_mnt/STM_Lab_Image_And_Data/DeepSeek_Lab/DeepSeek-R1-Distill-Llama-8B-310P-DUO/models/DeepSeek-R1-Distill-Llama-8B /home
fi


#CONTAINER="DeepSeek-R1-Distill-Llama-8B"
#docker_image="swr.cn-south-1.myhuaweicloud.com/ascendhub/mindie:2.0.RC2-300I-Duo-py311-openeuler24.03-lts"
docker run -itd --name $CONTAINER --ipc=host --net=host   \
--device=/dev/davinci0 \
--device=/dev/davinci1 \
--device=/dev/davinci2 \
--device=/dev/davinci3 \
--device=/dev/davinci4 \
--device=/dev/davinci5 \
--device=/dev/davinci6 \
--device=/dev/davinci7 \
--device=/dev/davinci_manager \
--device=/dev/devmm_svm \
--device=/dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/common:/usr/local/Ascend/driver/lib64/common \
-v /usr/local/Ascend/driver/lib64/driver:/usr/local/Ascend/driver/lib64/driver \
-v /etc/ascend_install.info:/etc/ascend_install.info \
-v /etc/vnpu.cfg:/etc/vnpu.cfg \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v $model_weight_path:$model_weight_path \
$docker_image \
 /bin/bash



# 检查容器是否运行
if ! docker ps -q -f name="$CONTAINER" | grep -q .; then
    echo "错误: 容器 '$CONTAINER' 未运行"
    exit 1
fi
docker exec -i  -e model_weight_path="$model_weight_path" -e host_ip=$host_ip -e modelname=$modelname "$CONTAINER" bash <<'EOF'
mindie_config_path="/usr/local/Ascend/mindie/latest/mindie-service/conf/config.json"
sed -i -E 's/"httpsEnabled" *: *true/"httpsEnabled" : false/' $mindie_config_path
#sed -i -E "s/"httpsEnabled" *: *true/"httpsEnabled" : false/" $mindie_config_path
sed -i -E "s|\"ipAddress\" : \"[0-9.]+\"|\"ipAddress\" : \"$host_ip\"|" $mindie_config_path
sed -i -E "s|\"modelWeightPath\" : \"[^\"]+\"|\"modelWeightPath\" : \"$model_weight_path\"|"  $mindie_config_path
sed -i -E "s|\"modelName\" : \"[^\"]+\"|\"modelName\" : \"$modelname\"|" $mindie_config_path

cd /usr/local/Ascend/mindie/latest/mindie-service/
nohup ./bin/mindieservice_daemon >/home/1.log 2>&1 &

# 可选：等待服务启动（根据实际情况调整）
sleep 5

# 验证服务是否运行
if ! pgrep -f mindieservice_daemon >/dev/null; then
    echo "错误: mindieservice_daemon 未成功启动"
    exit 1
fi

echo "服务已成功启动"
EOF

# 可选：检查日志文件
echo "查看服务日志: docker exec -it $CONTAINER tail -f /home/1.log"
sleep 5
echo $model_weight_path

#tail -f $model_weight_path/1.log


open_webui_imageid=$(docker images | grep "ghcr.nju.edu.cn/open-webui/open-webui" | awk '{print $1":"$2}')
if  [ ! "$(docker images | grep "ghcr.nju.edu.cn/open-webui/open-webui" | awk '{print $1":"$2}')"] ;then
     if [ ! -e "/home/open-webui.tar" ]; then 
          echo "不文件存在"
          scp -r root@$File_ip:/home/data_mnt/Images/open-webui.tar /home && {
        # scp成功后执行的命令
          docker load -i /home/open-webui.tar
          } || {
            # scp失败后执行的命令
            echo "scp传输失败，尝试从镜像源拉取..."
            docker pull  ghcr.nju.edu.cn/open-webui/open-webui:main

    }
   else 
        docker load -i /home/open-webui.tar
   fi
fi


docker run -d -p 3000:8080  -v open-webui:/app/backend/data -e OPENAI_API_KEY=NULL -e OPENAI_API_BASE_URL=http://$host_ip:1025/v1 --name open-webui --restart always ghcr.nju.edu.cn/open-webui/open-webui:main


docker exec -it $CONTAINER tail -f /home/1.log

#docker logs -f open-webui





